
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulebase.dto.ExamScheduleItemDto;
import com.rt.schedulebase.entity.ExamSchedule;
import com.rt.schedulebase.entity.ExamScheduleItem;
import com.rt.schedulebase.mapper.ExamScheduleItemMapper;
import com.rt.schedulebase.mapper.ExamScheduleMapper;
import com.rt.schedulenew.api.IExamScheduleItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ExamScheduleItemServiceImpl extends ServiceImpl<ExamScheduleItemMapper, ExamScheduleItem> implements IExamScheduleItemService {
    @Autowired
    private ExamScheduleMapper examScheduleMapper;
 
    public int getCount( ExamScheduleItemDto dto) {
         Map<String, Object> map = new HashMap<>();
        map.put("APPLY_NO", dto.getApplyNo());
        map.put("ITEM_NO", dto.getItemNo());
         QueryWrapper<ExamScheduleItem> qw = new QueryWrapper<>();
        qw.allEq(map, false);
        return baseMapper.selectCount(qw);
    }

    
    public List<ExamScheduleItem> selectList( ExamScheduleItemDto dto) {
         Map<String, Object> map = new HashMap<>();
        map.put("APPLY_NO", dto.getApplyNo());
        map.put("ITEM_NO", dto.getItemNo());
         QueryWrapper<ExamScheduleItem> qw = new QueryWrapper<>();
        qw.allEq(map, false);
         List<ExamScheduleItem> lsScheduleItem = baseMapper.selectList(qw);
         List<String> scheduleIds = new ArrayList<String>();
        lsScheduleItem.forEach(esi -> scheduleIds.add(esi.getScheduledId()));
         QueryWrapper<ExamSchedule> qw2 = new QueryWrapper<>();
        qw2.in("SCHEDULED_ID", scheduleIds);
        qw2.eq("SCHEDULE_STATUS", "00");
         List<ExamSchedule> lsSchedule = examScheduleMapper.selectList(qw2);
        if (lsSchedule.size() > 0) {
             List<String> scheduleIds2 = new ArrayList<String>();
            lsSchedule.forEach(es -> scheduleIds2.add(es.getScheduledId()));
            lsScheduleItem.removeIf(esi -> scheduleIds2.contains(esi.getScheduledId()));
        }
        return lsScheduleItem;
    }
    public int insertSelective(ExamScheduleItem entity) {
        return baseMapper.insert(entity);
    }
    public int update(ExamScheduleItem entity) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("APPLY_NO", entity.getApplyNo());
        map.put("ITEM_NO", entity.getItemNo());
        map.put("SCHEDULED_ID", entity.getScheduledId());
        UpdateWrapper<ExamScheduleItem> uw = new UpdateWrapper<>();
        uw.allEq(map, false);
        uw.set("".equals(entity.getExamStatus()), "EXAM_STATUS", "");
        return baseMapper.update(entity, uw);
    }
    public int updateByApplyNo( ExamScheduleItemDto dto) {
        ExamScheduleItem entity = new ExamScheduleItem();
        entity.setApplyNo(dto.getApplyNo());
        entity.setItemNo(dto.getItemNo());
         UpdateWrapper<ExamScheduleItem> uw = new UpdateWrapper<>();
        uw.eq("APPLY_NO", dto.getOldApplyNo());
        if (dto.getOldItemNo() != null) {
            uw.eq("ITEM_NO", dto.getOldItemNo());
        }
        return baseMapper.update(entity, uw);
    }
    public List<ExamScheduleItem> getItemList( ExamScheduleItem item) {
         Map<String, Object> map = new HashMap<>();
        map.put("SCHEDULED_ID", item.getScheduledId());
        map.put("APPLY_NO", item.getApplyNo());
        map.put("ITEM_NO", item.getItemNo());
         QueryWrapper<ExamScheduleItem> qw = new QueryWrapper<>();
        qw.allEq(map, false);
        return baseMapper.selectList(qw);
    }
    public int getExamScheduleItemSelectCount( String scheduleId) {
         QueryWrapper<ExamScheduleItem> query = new QueryWrapper<>();
        query.eq("SCHEDULED_ID", scheduleId);
        return baseMapper.selectCount(query);
    }
}
